package com.wx.controller;

import com.bean.Courier;
import com.bean.Message;
import com.bean.User;
import com.mvc.ResponseBody;
import com.service.CourierService;
import com.service.UserService;
import com.util.JSONUtil;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {
    @ResponseBody("/wx/loginSMS.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userPhone");
        CourierService courierService = new CourierService();
        UserService userService = new UserService();
        if (courierService.findByPhone(userPhone) != null || userService.findByPhone(userPhone) != null) {
            String code = "123456";
            boolean flag = true;
            Message msg = new Message();
            if (flag) {
                //短信发送成功
                msg.setStatus(0);
                msg.setResult("验证码已发送,请查收!");
            } else {
                //短信发送失败
                msg.setStatus(1);
                msg.setResult("验证码下发失败,请检查手机号或稍后再试");
            }
            UserUtil.setLoginSMS(request.getSession(), userPhone, code);

            String json = JSONUtil.toJSON(msg);
            return json;
        } else {
            String code = "123456";
            boolean flag = true;
            Message msg = new Message();
            if (flag) {
                //短信发送成功
                msg.setStatus(0);
                msg.setResult("成功注册该手机号且验证码已发送,请查收!");
                userService.insert(userPhone);
            } else {
                //短信发送失败
                msg.setStatus(1);
                msg.setResult("验证码下发失败,请检查手机号或稍后再试");
            }
            UserUtil.setLoginSMS(request.getSession(), userPhone, code);

            String json = JSONUtil.toJSON(msg);
            return json;
        }


    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String SMSCode = UserUtil.getLoginSMS(request.getSession(), userPhone);
        Message msg = new Message();
        if (SMSCode == null) {
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        } else if (SMSCode.equals(userCode)) {
            User user = new User();
            CourierService courierService = new CourierService();
            UserService userService = new UserService();
            Courier courier = courierService.findByPhone(userPhone);
            if (courier == null) {
                msg.setStatus(0);
                userService.updateLastLoginTime(userPhone);
                if (userService.findByPhone(userPhone).getUserName() != null)
                    user.setUserName(userService.findByPhone(userPhone).getUserName());
                user.setUser(true);
            } else {
                msg.setStatus(1);
                courierService.updateLastLoginTime(userPhone);
                user.setUserName(courier.getUserName());
                user.setUser(false);
            }
            msg.setResult("登录成功");
            user.setUserPhone(userPhone);
            UserUtil.setWxUser(request.getSession(), user);
        } else {
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response) {
        User user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if (isUser)
            msg.setStatus(0);
        else
            msg.setStatus(1);
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        //1.    销毁session
        request.getSession().invalidate();
        //2.    给客户端回复消息
        Message msg = new Message();
        msg.setStatus(0);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/showInfo.do")
    public String showInfo(HttpServletRequest request, HttpServletResponse response) {
        User wxUser = UserUtil.getWxUser(request.getSession());
        Message msg = new Message();
        msg.setData(wxUser);
        return JSONUtil.toJSON(msg);
    }
}
